package com.ljf.utility.domain.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljf.common.domain.service.BasisDomainService;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.utils.CollectionUtil;
import com.ljf.common.utils.EnumUtil;
import com.ljf.utility.domain.repository.UtilityDatCompileTabulationRepository;
import com.ljf.utility.dto.command.UtilityDatCompileTabulationCommand;
import com.ljf.utility.dto.query.UtilityDatCompileTabulationQuery;
import com.ljf.utility.enums.UtilityDatCompileTabulationStatusEnum;
import com.ljf.utility.model.UtilityDatCompileTabulation;
import com.ljf.utility.vo.UtilityDatCompileTabulationVO;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 编译目录表  领域服务
 *
 * @author LJF
 * @since 2025-08-30
 */
@Component
public class UtilityDatCompileTabulationDomainService extends BasisDomainService<UtilityDatCompileTabulationRepository, UtilityDatCompileTabulationQuery, UtilityDatCompileTabulationVO, UtilityDatCompileTabulation, UtilityDatCompileTabulationCommand> {

    @Override
    public UtilityDatCompileTabulation add(UtilityDatCompileTabulationCommand command) throws ServiceException {
        UtilityDatCompileTabulation model = UtilityDatCompileTabulation.builder()
                .name(command.getName())
                .status(UtilityDatCompileTabulationStatusEnum.NOT_VERSION.getCode())
                .versionCount(0)
                .build();
        repository.insert(model);
        return model;
    }

    @Override
    public Page<UtilityDatCompileTabulationVO> query(UtilityDatCompileTabulationQuery query) throws ServiceException {
        Page<UtilityDatCompileTabulationVO> page = super.query(query);
        if (CollectionUtil.isNotEmpty(page.getRecords())) {
            Map<Integer, String> statusMap = EnumUtil.getCodeToNameMap(UtilityDatCompileTabulationStatusEnum.values());
            for (UtilityDatCompileTabulationVO vo : page.getRecords()) {
                vo.setStatusName(statusMap.getOrDefault(vo.getStatus(), ""));
            }
        }
        return page;
    }

}
